// pages/cars/cars.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showList: [],
    willDelIdList: [],
    selNum: 0,
    totalPri: 0
  },

  // 删除购物车
  remove: function() {
    let hasStoraged = [];
    let currentDeletedList = this.data.willDelIdList;
    hasStoraged = wx.getStorageSync('hasAddGoods')
    for (let item1 = 0; item1 < hasStoraged.length; item1 ++) {
      for (let item2 = 0; item2 < currentDeletedList.length; item2 ++ ) {
        if (hasStoraged[item1].id == currentDeletedList[item2]) {
          hasStoraged.splice(item1,1);
        }
      }
    }
    this.setData({
      showList: hasStoraged,
    })
    wx.setStorage({
      data: hasStoraged,
      key: 'hasAddGoods',
    })
    let hasSel = this.data.willDelIdList.length;
    let hasSto = wx.getStorageSync('hasAddGoods');
    let pri = 0;
    for (let item1 of hasSto) {
      for (let item2 of this.data.willDelIdList) {
        if (item1.id == item2) {
          pri += item1.price;
        }
      }
    }
    this.setData({
      totalPri: pri,
      selNum: hasSel - hasSel,
    })
  },

  // 购物车有被更改时触发
  hasSomeChecked:function(e) {
    this.setData({
      willDelIdList: e.detail.value
    })
    let hasSel = this.data.willDelIdList.length;
    let hasSto = wx.getStorageSync('hasAddGoods');
    let pri = 0;
    for (let item1 of hasSto) {
      for (let item2 of this.data.willDelIdList) {
        if (item1.id == item2) {
          pri += item1.price;
        }
      }
    }
    this.setData({
      totalPri: pri,
      selNum: hasSel,
    })
  },

  // 全选
  selAll: function() {

  },

  // 阻止加减号冒泡同时处理总数
  preBub: function(e){
    let item = e.currentTarget.dataset.item;
    let symbel = e.currentTarget.dataset.addormi;
    let subtiList = wx.getStorageSync('hasAddGoods');
    let newList = [];
    if (symbel == "+") {
      newList = subtiList.map(x => {
        if (x.id == item.id) {
          return {
            id: x.id,
            imaUrl: x.imaUrl,
            disc: x.disc,
            price: x.price,
            collect: x.collect,
            selected: x.selected,
            total: x.total + 1,
          }
        } else {
          return x;
        }
      })
      wx.setStorageSync('hasAddGoods', newList);
      this.setData({
        showList: wx.getStorageSync('hasAddGoods'),
      })
    } else if (symbel == "-") {
      newList = subtiList.map(x => {
        if (x.id == item.id) {
          if (x.total < 1) {
            return x;
          } else {
            return {
              id: x.id,
              imaUrl: x.imaUrl,
              disc: x.disc,
              price: x.price,
              collect: x.collect,
              selected: x.selected,
              total: x.total - 1,
            }
          }
        } else {
          return x;
        }
      })
    }
    wx.setStorageSync('hasAddGoods', newList);
      let newPrice = 0;
      let newTotal = 0;
      for (let item of newList) {
        newPrice += item.price * item.total;
      }
      this.setData({
        showList: wx.getStorageSync('hasAddGoods'),
        totalPri: newPrice,
      })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    let haslogin = wx.getStorageSync('currentUser')
    if (!haslogin) {
      wx.setStorageSync('hasAddGoods', {})
    } else {
      wx.getStorage({
        key: 'hasAddGoods',
        success(res) {
          that.setData({
            showList: res.data,
          })
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})